package com.dnjn.happiness.auth.handler;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.dingtalk.open.app.api.GenericEventListener;
import com.dingtalk.open.app.api.OpenDingTalkStreamClientBuilder;
import com.dingtalk.open.app.api.message.GenericOpenDingTalkEvent;
import com.dingtalk.open.app.api.security.AuthClientCredential;
import com.dingtalk.open.app.stream.protocol.event.EventAckStatus;
import com.dnjn.happiness.auth.config.DingTalkConfig;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;

@Slf4j
@Component
public class DingTalkStreamHandler {

    static String topic = "/v1.0/im/bot/messages/get";

    @Autowired
    private DingTalkConfig config;

    @PostConstruct
    public void init(){
        log.info("init dingtalk stream ...");
        try {
            initDingTalkStream();
        } catch (Exception e) {
            log.error(e.getMessage());
        }
    }

    private void initDingTalkStream() throws Exception {

        OpenDingTalkStreamClientBuilder
                .custom()
                .credential(new AuthClientCredential(config.getClientId(), config.getClientSecret()))
                .registerAllEventListener(new GenericEventListener() {
                    @Override
                    public EventAckStatus onEvent(GenericOpenDingTalkEvent event) {
                        try {
                            log.info("dingtalk event{}",JSON.toJSONString(event));
                            //事件的唯一Id
                            String eventId = event.getEventId();
                            //事件类型
                            String eventType = event.getEventType();
                            //事件产生时间
                            Long bornTime = event.getEventBornTime();
                            //获取事件体
                            JSONObject bizData = event.getData();
                            //处理事件
                            process(bizData);
                            //消费成功
                            return EventAckStatus.SUCCESS;
                        } catch (Exception e) {
                            //消费失败
                            return EventAckStatus.LATER;
                        }
                    }
                })
                //注册机器人监听器
                .registerCallbackListener(topic, robotMessage -> {
                    log.info("receive robotMessage, {}", robotMessage);
                    //开发者根据自身业务需求，处理机器人回调
                    return new JSONObject();

                })
//                //注册卡片回传监听器
//                .registerCallbackListener("/v1.0/card/instances/callback", callbackData -> {
//                    log.info("receive call back request, {}", callbackData);
//                    //your code is here
//                    //开发者根据自身业务需求，变更卡片内容，返回response
//                    CardCallbackResponse resp = new CardCallbackResponse();
//                    return resp;
//
//                })
                .build().start();
    }

    private   void process(JSONObject bizData) {
        //todo
    }
}
